package huoye.shops.huoye.user.activity;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;


import com.codeideology.android.utils.ApkUtils;
import com.codeideology.android.utils.StringUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

import commonality.updater.UpdateAppManager;
import huoye.shops.MainActivity;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.base.UserManager;
import huoye.shops.common.model.SMSCoder;
import huoye.shops.huoye.user.model.UpdateInfo;
import huoye.shops.huoye.user.model.User;
import huoye.shops.huoye.user.presenter.LoginPresenter;
import huoye.shops.huoye.user.view.ILoginView;
import huoye.shops.widget.UpdateDialog;


/**
 * Created by user on 2017/11/21.
 */

public class LoginActivity extends BaseActivity implements ILoginView {



    private LoginPresenter mLoginPresenter;

    @BindView(R.id.login_et_accout)
    EditText etAccout;

    @BindView(R.id.login_et_code)
    EditText etCode;

    @BindView(R.id.login_btn_get_code)
    Button btnGetCode;

    @BindView(R.id.login_btn_login)
    Button btnLogin;

    @BindView(R.id.include_head_new_iv_back)
    ImageView iv;

    @OnClick(R.id.include_head_new_iv_back)
    void onBack() {
        startActivity(SelectNewActivity.class);
        finish();
    }
    private static final int REQUEST_CODE_WRITE_STORAGE = 1002;
    private static final int REQUEST_CODE_UNKNOWN_APP = 2001;

    private String mAccout ="";
    private String mCodeId = "";
    private String mCode = "";

    @OnClick(R.id.login_btn_get_code)
    void onGetCodeClick() {
        mAccout = etAccout.getText().toString();
        mLoginPresenter.senCode(mAccout);
    }

    @OnClick(R.id.login_btn_login)
    void onConfirmClick() {
           mCode = etCode.getText().toString();
          mAccout = etAccout.getText().toString();
          if(mAccout.length() != 11){
              makeToast("手机号码无效");
              return;
          }
           Log.e("test",mCodeId);
           mLoginPresenter.login(mAccout,mCodeId,mCode);

    }



    String imgPath ="https://goss.veer.com/creative/vcg/veer/612/veer-132857661.jpg";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {

        mLoginPresenter = new LoginPresenter();
        mLoginPresenter.addTaskListener(this);
        mLoginPresenter.onStart();

   /*     if(StringUtils.isNotEmpty(UserManager.getInstance().getUserId())){
            startActivity(BuildEventActivity.class);
        }*/

        //版本更新
     // mLoginPresenter.checkUpdate();

    //    makeToast(UserManager.getInstance().getCity());
        //  startActivity(BuildEventActivity.class);

     /* if(StringUtils.isNotEmpty(UserManager.getInstance().getUserId())){
            startActivity(MainActivity.class);
        }*/
    }


    @Override
    public void codeByoyomi(String text, boolean IsClickable) {
        btnGetCode.setText(text);
        btnGetCode.setClickable(IsClickable);
        btnGetCode.setSelected(IsClickable);
    }

    @Override
    public void getCodeData(SMSCoder smsCoder) {
        mCodeId = smsCoder.getSendId();
        etCode.requestFocus();
        etCode.setSelection(etCode.getText().length());
        Log.e("test",smsCoder.toString());
    }


    @Override
    public void onCheckUpdate(UpdateInfo info) {
        // 检查升级
        showUpdate(info);
    }


    /**
     * 升级提示
     *
     * @param info
     */
    private void showUpdate(final UpdateInfo info) {

        String versionName = ApkUtils.getVersionName(this);
        Log.e("test",versionName);
        if(Float.valueOf(versionName) >= Float.valueOf(info.getAppVerno())){
            if(StringUtils.isNotEmpty(UserManager.getInstance().getUserId())){
                startActivity(MainActivity.class);
                finish();
            }
        }else {
            showDialogUpdater(info);
        }

    }



    private void showDialogUpdater(UpdateInfo info){
        UpdateDialog updateDialog = new UpdateDialog(this);
        updateDialog.setOnUpdateListener(new UpdateDialog.OnUpdateListener() {
            @Override
            public void onUpdateClick() {
                update(info);
            }
        });
        updateDialog.show();
        updateDialog.setDialogContent(info.getAppVerno()+"",info.getSize(),info.getUpContent());
    }

    private void update(UpdateInfo info){
        UpdateAppManager.downloadApkS(this, info.getDownUrl(), getResources().getString(R.string.app_name), "帮播侠");

    }








}


